var express = require('express');
var router = express.Router();
var encryption = require('encryption');
/* GET home page. */
var crypto = require('crypto');

function sortEach(dict) {
    var keys = dict.sort();
    return keys.join('');
};


function sha1(str) {
    var md5sum = crypto.createHash('sha1');
    md5sum.update(str);
    str = md5sum.digest('hex');
    return str;
}


var GET = function(req, res, next){

    var query = req.query;
    var token = 'gwscode';
    var signature = query.signature;
    var timestamp = query.timestamp;
    var nonce = query.nonce;
    var echostr = query.echostr;

    var _token = sortEach([token, timestamp, nonce]);

    _token = sha1(_token);

    console.log(_token,'---',signature);

    if(_token === signature)
        res.send(echostr);
    else res.send('error');
};


router.get('/', GET);

module.exports = router;

